package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.RepairAcceptDetailMapper;
import com.zygh.policeasset.mapper.RepairAcceptMapper;
import com.zygh.policeasset.mapper.RepairMapper;
import com.zygh.policeasset.pojo.RepairAccept;
import com.zygh.policeasset.pojo.RepairAcceptDTO;
import com.zygh.policeasset.pojo.RepairAcceptDetail;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.vo.RepairAcceptDetailVO;
import com.zygh.policeasset.pojo.vo.RepairAcceptVO;
import com.zygh.policeasset.pojo.vo.RepairVO;
import com.zygh.policeasset.utils.AuditUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class RepairAcceptService {

    @Autowired
    RepairAcceptMapper repairAcceptMapper;

    @Autowired
    RepairMapper repairMapper;

    @Autowired
    RepairAcceptDetailMapper repairAcceptDetailMapper;

    @Autowired
    AuditFlowService auditFlowService;

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult add(RepairAcceptDTO repairAcceptDTO) {

        try {

            String now = DateUtil.now();
            String flowNo = AuditUtil.getFlowNo(now);
            RepairAccept repairAccept = new RepairAccept();
            repairAccept.setRepairIds(repairAcceptDTO.getRepairIds());
            repairAccept.setType(repairAcceptDTO.getType());
            repairAccept.setContent(repairAcceptDTO.getContent());
            repairAccept.setOrderUserId(repairAcceptDTO.getOrderUserId());
            repairAccept.setFlowNo(flowNo);
            int count = repairAcceptMapper.add(repairAccept);
            auditFlowService.add(repairAcceptDTO.getOrderUserId(), flowNo, now,
                    TableType.REPAIRACCEPT.getName(), TableType.REPAIRACCEPT.getType(),
                    null, null);
            for (RepairAcceptDetail repairAcceptDetail:repairAcceptDTO.getRepairAcceptDetailList()) {
                repairAcceptDetail.setRepairAcceptId(repairAccept.getId());
            }
            repairAcceptDetailMapper.add(repairAcceptDTO.getRepairAcceptDetailList());
            if (count > 0) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
        return new CommonResult().validateFailed("添加失败");
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult delete(Integer id) {

        try {

            int count = repairAcceptMapper.delete(id);

            repairAcceptDetailMapper.deleteByRepairAcceptId(id);

            if (count > 0) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult update(RepairAcceptDTO repairAcceptDTO) {

        try {

            RepairAccept repairAccept = new RepairAccept();
            repairAccept.setId(repairAcceptDTO.getId());
            repairAccept.setRepairIds(repairAcceptDTO.getRepairIds());
            repairAccept.setType(repairAcceptDTO.getType());
            repairAccept.setContent(repairAcceptDTO.getContent());
            repairAccept.setOrderUserId(repairAcceptDTO.getOrderUserId());
            int count = repairAcceptMapper.updateById(repairAccept);

            repairAcceptDetailMapper.deleteByRepairAcceptId(repairAcceptDTO.getId());

            repairAcceptDetailMapper.add(repairAcceptDTO.getRepairAcceptDetailList());

            if (count > 0) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
        return new CommonResult().validateFailed("删除失败");
    }

    public CommonResult findAll(int pageNum, int pageSize, Integer userId,Integer status) {

        try {
            PageHelper.startPage(pageNum, pageSize);
            List<RepairAcceptVO> repairAcceptList = repairAcceptMapper.findall(userId,status);

            for (RepairAcceptVO repairAcceptVO : repairAcceptList) {

                List<RepairAcceptDetailVO> repairAcceptDetailList = repairAcceptDetailMapper.findallByRepairAcceptId(repairAcceptVO.getId());
                if (repairAcceptDetailList.size() > 0) {
                    repairAcceptVO.setRepairAcceptDetailList(repairAcceptDetailList);
                }

                List<RepairVO> repairVOList = repairMapper.findByIds(repairAcceptVO.getRepairIds().split(","));
                if (repairAcceptDetailList.size() > 0) {
                    repairAcceptVO.setRepairVOList(repairVOList);
                }
            }
            return new CommonResult().success(new PageInfo<>(repairAcceptList));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public CommonResult selectById(Integer id) {

        try {
            RepairAcceptVO repairAcceptVO = repairAcceptMapper.selectById(id);

            List<RepairAcceptDetailVO> repairAcceptDetailList = repairAcceptDetailMapper.findallByRepairAcceptId(repairAcceptVO.getId());
            if (repairAcceptDetailList.size() > 0) {
                repairAcceptVO.setRepairAcceptDetailList(repairAcceptDetailList);
            }
            return new CommonResult().success(repairAcceptVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }
}
